﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.NLogger
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using NLog;
using System;
using System.Text;

#nullable enable
namespace Commom.Util;

public class NLogger
{
  private static readonly Logger logger = LogManager.GetCurrentClassLogger();

  public static void Debug(string msg, string userInfo = "")
  {
    string str = string.Empty;
    if (userInfo != "")
      str = userInfo;
    string message = $"{str}\r\n{msg}";
    NLogger.logger.Debug(message);
  }

  public static void Info(string msg, string userInfo = "")
  {
    string str = string.Empty;
    if (userInfo != "")
      str = userInfo;
    string message = $"{str}\r\n{msg}";
    NLogger.logger.Info(message);
  }

  public static void Warn(string msg, string userInfo = "")
  {
    string str = string.Empty;
    if (userInfo != "")
      str = userInfo;
    string message = $"{str}\r\n{msg}";
    NLogger.logger.Warn(message);
  }

  public static void Error(string msg, string userInfo = "")
  {
    string str = string.Empty;
    if (userInfo != "")
      str = userInfo;
    string message = $"{str}\r\n{msg}";
    NLogger.logger.Error(message);
  }

  public static void Error(Exception ex, string userInfo = "")
  {
    string str = "";
    if (userInfo != "")
      str = userInfo;
    string message = $"{str}\r\n{NLogger.ErrorDetails(ex)}";
    NLogger.logger.Error(message);
  }

  private static string ErrorDetails(Exception ex)
  {
    StringBuilder stringBuilder = new StringBuilder();
    int num = 0;
    string str = "";
    for (; ex != null; ex = ex.InnerException)
    {
      if (num > 0)
        str += "　";
      stringBuilder.AppendLine($"{str} <br>异常消息：{ex.Message}");
      stringBuilder.AppendLine($"{str} <br>异常类型：{ex.GetType().FullName}");
      stringBuilder.AppendLine($"{str} <br>异常源：{ex.Source}");
      if (ex.StackTrace != null)
        stringBuilder.AppendLine($"{str}<br>异常堆栈：{ex.StackTrace}");
      if (ex.InnerException != null)
      {
        stringBuilder.AppendLine(str + "<br>内部异常：");
        ++num;
      }
    }
    return stringBuilder.ToString().Replace("位置:", "<br>位置");
  }

  public static void Fatal(string msg, string userInfo = "")
  {
    string str = string.Empty;
    if (userInfo != "")
      str = userInfo;
    string message = $"{str}\r\n{msg}";
    NLogger.logger.Fatal(message);
  }

  public static void Trace(string msg, string userInfo = "")
  {
    string str = string.Empty;
    if (userInfo != "")
      str = userInfo;
    string message = $"{str}\r\n{msg}";
    NLogger.logger.Trace(message);
  }
}
